package com.test.demos.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.test.demos.pojo.dto.AppointmentsDTO;
import com.test.demos.pojo.dto.AppointmentsNewDTO;
import com.test.demos.pojo.entity.Appointments;
import com.test.demos.mapper.AppointmentsMapper;
import com.test.demos.pojo.vo.AppointmentsVO;
import com.test.demos.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.test.demos.service.AppointmentsService;

import java.util.ArrayList;
import java.util.List;

import static com.test.demos.pojo.entity.table.AppointmentsTableDef.APPOINTMENTS;
import static org.springframework.util.ObjectUtils.isEmpty;

/**
 * (Appointments)表服务接口
 *
 * @author twt
 * @since 2025-04-14 16:25:36
 */
@Service
@Transactional
public class AppointmentsServiceImpl implements AppointmentsService {

    @Autowired
    private AppointmentsMapper appointmentsMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param appointmentId 主键
     * @return 实例对象
     */
    @Override
    public Appointments queryById(Integer appointmentId) {
        Appointments select = appointmentsMapper.selectOneById(appointmentId);
        if (isEmpty(select)) {
            throw new RuntimeException("未查询到对应数据");
        }
        return select;
    }


    @Override
    public Result booking(AppointmentsDTO dto) {
        //查询预约记录是否存在
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper
                .where(APPOINTMENTS.PACKAGE_ID.eq(dto.getPackageId())
                        .and(APPOINTMENTS.USER_ID.eq(dto.getUserId())));
        Appointments select = appointmentsMapper.selectOneByQuery(queryWrapper);
        if (!isEmpty(select)) {
            return Result.error("预约记录已存在，无法重复预约");
        }
        Appointments appointments = new Appointments();
        BeanUtil.copyProperties(dto, appointments);
        appointments.setStatus("1");
        appointmentsMapper.insert(appointments);
        return Result.ok();
    }

    @Override
    public AppointmentsVO myAppointments(Integer loginId) {


        //2.查询预约记录
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper
               .where(APPOINTMENTS.USER_ID.eq(loginId));
        List<Appointments> appointmentsList = appointmentsMapper.selectListByQuery(queryWrapper);
        System.out.println("appointmentsList = " + appointmentsList);
        if (isEmpty(appointmentsList)) {
            return null;
        }
        AppointmentsVO appointmentsVO = new AppointmentsVO();
        List<AppointmentsNewDTO>  appointmentsNewDTOList = new ArrayList<>();
        appointmentsList.forEach(appointments -> {
            AppointmentsNewDTO appointmentsNewDTO = new AppointmentsNewDTO();
            BeanUtil.copyProperties(appointments, appointmentsNewDTO);
            appointmentsNewDTOList.add(appointmentsNewDTO);
        });
        appointmentsVO.setUserId(loginId);
        appointmentsVO.setAppointmentsNewDTOList(appointmentsNewDTOList);
        return appointmentsVO;
    }

    @Override
    public List<Appointments> queryAll() {
        return appointmentsMapper.selectAll();
    }


    /**
     * 根据主键删除数据
     *
     * @param appointmentId
     */
    @Override
    public void deleteById(Integer appointmentId) {
        int i = appointmentsMapper.deleteById(appointmentId);
        if (i != 1) {
            throw new RuntimeException("删除数据失败");
        }
    }

}
